#ifndef CONNECTIONPOOL_H
#define CONNECTIONPOOL_H

#include <string>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <memory>

#include "../base/Singleton.h"

/// @brief 
/// @todo 实现动态大小的连接池
class ConnectionPool : public Singleton<ConnectionPool>
{
    friend Singleton<ConnectionPool>;

public:
    ConnectionPool(const ConnectionPool &) = delete;
    ConnectionPool &operator=(const ConnectionPool &) = delete;

    void init(std::string ip, uint16_t port, std::string user, std::string password, std::string database,
              int maxSize, int timeout);

    std::shared_ptr<sql::Connection> getConnection();

    int getQueueSize() {return m_connection_queue.size();}

private:
    ConnectionPool();

    void addConnection();

    std::string m_ip;
    uint16_t m_port;
    std::string m_user;
    std::string m_password;
    std::string m_database;

    int m_max_size;
    int m_timeout;

    std::queue<sql::Connection*> m_connection_queue;
    std::mutex m_mutex;
    std::condition_variable m_cond;

    sql::Driver *m_driver;
};

#endif // CONNECTIONPOOL_H